"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能
Case Name   : 升主过程中，dss出现故障
Create At   : 2023/04/13
Owner       : opentestcase043
Description :
        1.查询集群状态
        2.kill -9 dss进程
        3.进行switchover操作
        4.恢复dss目录，进行switchover操作
        5.查看集群状态
        6.kill -19 dss进程
        7.进行switchover操作
        8.还原dss目录
        9.进行switchover操作，查看集群状态
        10.清理环境
Expect      :
        1.集群状态正常
        2.kill dss进程成功
        3.switchover操作失败
        4.cm自动选主成功
        5.集群状态正常
        6.kill dss进程成功
        7.switchover操作失败，集群状态异常
        8.成功
        9.进行switchover操作成功，集群状态正常
        10.清理环境成功
History     :
"""

import os
import time
import unittest

from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Logger import Logger
from yat.test import macro
from yat.test import Node

LOG = Logger()
Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorage(unittest.TestCase):

    def setUp(self):
        LOG.info('-----this is setup-----')
        LOG.info(f'-----{os.path.basename(__file__)} start-----')
        self.con = Constant()
        self.com = Common()
        self.pri_node = Node('PrimaryDbUser')

    def test_share_storage(self):
        text = '-----step1:查询集群状态; expect:集群状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)

        text = '-----step2:kill -9 dss进程; expect:kill dss进程成功-----'
        LOG.info(text)
        pri_dss_pid = self.com.get_pid(self.pri_node, 'dssserver')
        cmd = f'mv {macro.DSS_INSTANCE} {macro.DSS_INSTANCE}_bak'
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        LOG.info(status)
        self.assertEqual('', status, '执行失败:' + text)
        cmd = f"kill -9 {pri_dss_pid}"
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        LOG.info(status)
        self.assertEqual('', status, '执行失败:' + text)

        text = '-----step3:进行switchover操作; expect:switchover操作失败-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('switchover', '-A')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_timeout_msg, status,
                      '执行失败:' + text)

        text = '-----step4:恢复dss目录，进行switchover操作; '\
               'expect:cm自动选主成功-----'
        LOG.info(text)
        cmd = f'''rm -rf {macro.DSS_INSTANCE};
            mv {macro.DSS_INSTANCE}_bak {macro.DSS_INSTANCE}'''
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        LOG.info(status)
        self.assertEqual('', status, '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('start')
        LOG.info(status)
        self.assertIn(self.con.cm_start_success_msg, status,
                      '执行失败:' + text)
        time.sleep(40)
        status = Primary_SH.exec_cm_ctl('switchover', '-A')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_A_success_msg, status,
                      '执行失败:' + text)
        ret = Primary_SH.exec_cm_ctl('ddb', '--get / --prefix')
        node_name = str(ret).split('\n')[22].strip()
        if node_name == '6001':
            status = Primary_SH.exec_cm_ctl('switchover', '-A')

        text = '-----step5:查看集群状态; expect:集群状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)

        text = '-----step6:kill -19 dss进程; expect:kill dss进程成功-----'
        LOG.info(text)
        pri_dss_pid = self.com.get_pid(self.pri_node, 'dssserver')
        self.assertNotEqual('', pri_dss_pid, '执行失败:' + text)
        cmd = f'mv {macro.DSS_INSTANCE} {macro.DSS_INSTANCE}_bak'
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        LOG.info(status)
        self.assertEqual('', status, '执行失败:' + text)
        cmd = f"kill -19 {pri_dss_pid}"
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        LOG.info(status)
        self.assertEqual('', status, '执行失败:' + text)

        text = '-----step7:进行switchover操作; '\
               'expect:switchover执行超时，集群状态异常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertIn('switchover command timeout', status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertNotIn('cluster_state   : Normal', status,
                      '执行失败:' + text)

        text = '-----step8:还原dss目录; expect:成功-----'
        LOG.info(text)
        cmd = f'''rm -rf {macro.DSS_INSTANCE};
            mv {macro.DSS_INSTANCE}_bak {macro.DSS_INSTANCE}'''
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        LOG.info(status)
        self.assertEqual('', status, '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('start')
        LOG.info(status)

        text = '-----step9:进行switchover操作，查看集群状态; '\
               'expect:进行switchover操作成功，集群状态正常-----'
        LOG.info(text)
        pri_dss_pid = self.com.get_pid(self.pri_node, 'dssserver')
        cmd = f"kill -18 {pri_dss_pid}"
        LOG.info(cmd)
        status = self.pri_node.sh(cmd).result()
        LOG.info(status)
        self.assertEqual('', status, '执行失败:' + text)
        time.sleep(10)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)

    def tearDown(self):
        text = '-----this is tearDown-----'
        LOG.info(text)
        text = '-----step10:清理环境; expect:清理环境成功-----'
        LOG.info(text)
        status1 = Primary_SH.exec_cm_ctl('start')
        status2 = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status1)
        LOG.info(status2)
        self.assertIn(self.con.cm_start_success_msg, status1,
                      '执行失败:' + text)
        self.assertIn(self.con.cm_switchover_success_msg, status2,
                      '执行失败:' + text)
        LOG.info(f'-----{os.path.basename(__file__)} end-----')
